/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.autoconfigure.healthcheck;

import com.lvyh.lightframe.healthcheck.cache.ApplicationAvailabilityCache;
import com.lvyh.lightframe.healthcheck.checker.HealthChecker;
import com.lvyh.lightframe.healthcheck.checker.impl.SimpleHealthChecker;
import com.lvyh.lightframe.healthcheck.common.SpringContextUtil;
import com.lvyh.lightframe.healthcheck.processor.LivenessStateListener;
import com.lvyh.lightframe.healthcheck.processor.ReadinessStateListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Health check registration,register two kinds of listening and processing classes.
 *
 * @author lvyh 21/03/06.
 */
@Configuration
public class HealthCheckConfig {

    @Bean
    public SpringContextUtil springContextUtil() {
        return new SpringContextUtil();
    }

    @Bean
    @ConditionalOnClass(SimpleHealthChecker.class)
    public ApplicationAvailabilityCache applicationAvailabilityCache() {
        return new ApplicationAvailabilityCache();
    }

    @Bean
    public LivenessStateListener livenessStateListener() {
        return new LivenessStateListener();
    }

    @Bean
    public ReadinessStateListener readinessStateListener() {
        return new ReadinessStateListener();
    }

    @Bean
    @ConditionalOnMissingBean
    public HealthChecker simpleHealthChecker() {
        return new SimpleHealthChecker();
    }
}
